//
//  LoginView.m
//
//
//  Created by bh on 16/12/28.
//  Copyright © 2016年 BH. All rights reserved.
//

#import "LoginView.h"
#import "BHMainTabbarController.h"

 static CGFloat loginBgViewH = 88;

@implementation LoginView

- (instancetype)initWithFrame:(CGRect)frame
{


    self = [super initWithFrame:frame];
    if (self) {
        
        //整个界面背景
        self.loginBgImg = [[UIImageView alloc]init];
        [self addSubview:self.loginBgImg];
        self.loginBgImg.image = [UIImage imageNamed:@"bg_sign_in"];
        self.loginBgImg.userInteractionEnabled = YES;
        
        [self.loginBgImg mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.left.right.equalTo(self).with.offset(0);
            make.height.mas_equalTo(BHscreenH);
        }];
        
        //logo
        self.logoIconImg = [[UIImageView alloc]init];
        [self addSubview:self.logoIconImg];
        self.logoIconImg.image = [UIImage imageNamed:@"ico_sign_in_logo"];
        
        [self.logoIconImg mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self).with.offset(64*UIScreenHP);
            make.centerX.equalTo(self).with.offset(0);
            make.size.mas_equalTo(CGSizeMake(180, 108.5));
        }];
        
        //半透明背景框
        self.bg_transparentView = [[UIImageView alloc]init];
        [self.loginBgImg addSubview:self.bg_transparentView];
        self.bg_transparentView.image = [UIImage imageNamed:@"bg_transparent"];
        self.bg_transparentView.userInteractionEnabled = YES;
        
        [self.bg_transparentView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.logoIconImg.mas_bottom).with.offset(30);
            make.centerX.equalTo(self.loginBgImg).with.offset(0);
            make.size.mas_equalTo(CGSizeMake(BHscreenW - 30, 270));
        }];
        
        //用户名背景框
        UIView * userBgView = [[UIView alloc]init];
        [self.bg_transparentView addSubview:userBgView];
        userBgView.backgroundColor = [UIColor whiteColor];
        userBgView.userInteractionEnabled = YES;
        
        [userBgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.leading.equalTo(self.bg_transparentView).with.offset(25);
            make.top.equalTo(self.bg_transparentView).with.offset(25);
            make.size.mas_equalTo(CGSizeMake(BHscreenW - 80, loginBgViewH/2));
        }];
        
        self.userIcon = [[UIImageView alloc]init];
        [userBgView addSubview:self.userIcon];
        self.userIcon.image = [UIImage imageNamed:@"ico_sign_in_number"];
        
        userBgView.layer.cornerRadius= 5;
        userBgView.layer.masksToBounds = YES;
        
        
        [self.userIcon mas_makeConstraints:^(MASConstraintMaker *make) {
            make.leading.equalTo(userBgView).with.offset(15);
            make.centerY.equalTo(userBgView).with.offset(0);
            make.size.mas_equalTo(CGSizeMake(25, 25));
        }];
        
        //密码背景框
        UIView * passwordBgView = [[UIView alloc]init];
        [self.bg_transparentView addSubview:passwordBgView];
        passwordBgView.backgroundColor = [UIColor whiteColor];
        
        [passwordBgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.leading.equalTo(userBgView).with.offset(0);
            make.top.equalTo(userBgView.mas_bottom).with.offset(12);
            make.size.mas_equalTo(CGSizeMake(BHscreenW - 80, loginBgViewH/2));
        }];
        
        passwordBgView.layer.cornerRadius= 5;
        passwordBgView.layer.masksToBounds = YES;
        
        self.passwordIcon = [[UIImageView alloc]init];
        [passwordBgView addSubview:self.passwordIcon];
        self.passwordIcon.image = [UIImage imageNamed:@"ico_sign_in_password"];
        
        [self.passwordIcon mas_makeConstraints:^(MASConstraintMaker *make) {
            make.leading.equalTo(passwordBgView).with.offset(15);
            make.centerY.equalTo(passwordBgView).with.offset(0);
            make.size.mas_equalTo(CGSizeMake(25, 25));
        }];
        
        //用户名
        self.accountTF = [[UITextField alloc]init];
        [userBgView addSubview:self.accountTF];
      
        self.accountTF.placeholder = @"请输入用户名";
        self.accountTF.userInteractionEnabled = YES;
        self.accountTF.enabled = YES;

        self.accountTF.clearButtonMode = UITextFieldViewModeWhileEditing;
        

        [self.accountTF mas_makeConstraints:^(MASConstraintMaker *make) {
            make.leading.equalTo(self.userIcon.mas_trailing).with.offset(15);
             make.centerY.equalTo(userBgView).with.offset(0);
            make.size.mas_equalTo(CGSizeMake(BHscreenW - 80, 44));
        }];
        
        //登录密码
        self.passwordTF = [[UITextField alloc]init];
        [passwordBgView addSubview:self.passwordTF];
        self.passwordTF.placeholder = @"请输入6-16位的密码";
        self.passwordTF.secureTextEntry = YES;
        self.passwordTF.userInteractionEnabled = YES;
        
      //  self.passwordTF.clearsOnBeginEditing = YES;
        self.passwordTF.clearButtonMode = UITextFieldViewModeWhileEditing;
        
        [self.passwordTF mas_makeConstraints:^(MASConstraintMaker *make) {
            make.leading.equalTo(self.passwordIcon.mas_trailing).with.offset(15);
            make.centerY.equalTo(passwordBgView).with.offset(0);
            make.size.mas_equalTo(CGSizeMake(BHscreenW - 80, 44));
        }];
        
        //记住密码
        self.remenberPwBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [self addSubview:self.remenberPwBtn];
        [self.remenberPwBtn setBackgroundImage:[UIImage imageNamed:@"btn_sign_in_onselected"] forState:UIControlStateNormal];
        [self.remenberPwBtn setBackgroundImage:[UIImage imageNamed:@"btn_sign_in_unselected"] forState:UIControlStateSelected];
        
        self.remenberPwBtn.tag = 100;
        
        
        [self.remenberPwBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.leading.equalTo(self.bg_transparentView).with.offset(32.5);
            make.top.equalTo(self.passwordTF.mas_bottom).with.offset(15);
            make.size.mas_equalTo(CGSizeMake(20, 20));
        }];
        
        UILabel * remenberLabel = [[UILabel alloc]init];
        [self addSubview:remenberLabel];
        remenberLabel.text = @"记住密码";
        remenberLabel.textColor = BHColor(0, 255, 175);
        remenberLabel.font = BHFont(13);
        
        [remenberLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.leading.equalTo(self.remenberPwBtn.mas_trailing).with.offset(20);
            make.top.equalTo(self.remenberPwBtn).with.offset(-5);
            make.size.mas_equalTo(CGSizeMake(100, 30));
        }];
        
        UILabel * autoLoginLabel = [[UILabel alloc]init];
        [self addSubview:autoLoginLabel];
        autoLoginLabel.text = @"自动登录";
        autoLoginLabel.textColor = BHColor(0, 255, 175);
        autoLoginLabel.font = BHFont(13);
        
        [autoLoginLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.trailing.equalTo(self.bg_transparentView).with.offset(10);
            make.top.equalTo(self.remenberPwBtn).with.offset(0);
            make.size.mas_equalTo(CGSizeMake(100, 20));
        }];

        
        //自动登录
        self.autoLoginBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [self addSubview:self.autoLoginBtn];
        
        [self.autoLoginBtn setBackgroundImage:[UIImage imageNamed:@"btn_sign_in_onselected"] forState:UIControlStateNormal];
        [self.autoLoginBtn setBackgroundImage:[UIImage imageNamed:@"btn_sign_in_unselected"] forState:UIControlStateSelected];
        
        [self.autoLoginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.leading.equalTo(autoLoginLabel).with.offset(-40);
            make.top.equalTo(self.remenberPwBtn).with.offset(0);
            make.size.mas_equalTo(CGSizeMake(20, 20));
        }];

        self.autoLoginBtn.tag = 101;
        
        //登录
        self.loginButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [self addSubview:self.loginButton];
        [self.loginButton setTitle:@"登录" forState:UIControlStateNormal];
        [self.loginButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        self.loginButton.titleLabel.font = BHFont(18);
        self.loginButton.backgroundColor = BHColor(0, 168, 255);
        
        self.loginButton.layer.cornerRadius = 4;
        self.loginButton.layer.masksToBounds = YES;
        

        self.loginButton.tag = 102;
        
        [self.loginButton mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.size.mas_equalTo(CGSizeMake(BHscreenW - 84, 44));
            make.leading.equalTo(self.bg_transparentView).with.offset(29);
            make.top.equalTo(self.remenberPwBtn.mas_bottom).with.offset(35);
            
        }];
        
        
//        NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
//        [defaults setObject:@"1" forKey:RemberPassword];
//        NSUserDefaults * autoLogins = [NSUserDefaults standardUserDefaults];
//        [autoLogins setObject:@"1" forKey:AutoLogin];
//        
//        //用于判断是否记住密码
//        NSUserDefaults * defaul = [NSUserDefaults standardUserDefaults];
//        NSString * rember = [defaul objectForKey:RemberPassword];
//        NSString * autoLogin = [defaul objectForKey:AutoLogin];
//        
//        //记住密码
//        if([rember isEqualToString:@"1"]||[autoLogin isEqualToString:@"1"]){
//            self.accountTF.text = [defaul objectForKey:Account];
//            self.passwordTF.text = [defaul objectForKey:Password];
//            self.remenberPwBtn.selected = YES;
//        }else if([rember isEqualToString:@"0"]||[autoLogin isEqualToString:@"0"]){
//            self.remenberPwBtn.selected = NO;
//        }
//        //自动登录
//        if([autoLogin isEqualToString:@"1"]){
//            self.autoLoginBtn.selected = YES;
//        }else if ([autoLogin isEqualToString:@"0"]){
//            self.autoLoginBtn.selected = NO;
//        }
        

    }
    return self;
}

//#pragma mark - 点击事件
//- (void)buttonClick:(UIButton *)button
//{
//    if(button.tag == 100)
//    { //----记住密码
//     
//        [self remberPassWordWith:button];
//        
//    }else if(button.tag == 101)
//    {//-----自动登录
//       
//        [self autoLoginWith:button];
//    }
//    else if (button.tag == 102)
//    {//-----登录
//        [self loginWith:button];
//    }
//
//}
////记住密码
//-(void)remberPassWordWith:(UIButton *)button{
//    if(button.selected == YES){
//        button.selected = NO;
//        NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
//        [defaults setObject:@"0" forKey:RemberPassword];
//        
//        UIButton * button2 = (UIButton *)[self viewWithTag:200];
//        button2.selected = YES;
//        [defaults setObject:@"0" forKey:AutoLogin];
//    }else{
//        button.selected = YES;
//        NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
//        [defaults setObject:@"1" forKey:RemberPassword];
//        
//    }
//    [self remberPassWord];
//}
//
////自动登录
//-(void)autoLoginWith:(UIButton *)button{
//    if(button.selected == YES){ //选择自动登录
//        button.selected = NO;
//        NSUserDefaults * autoLogin = [NSUserDefaults standardUserDefaults];
//        [autoLogin setObject:@"0" forKey:AutoLogin];
//    }else{//取消自动登录
//        button.selected = YES;
//        NSUserDefaults * autoLogin = [NSUserDefaults standardUserDefaults];
//        [autoLogin setObject:@"1" forKey:AutoLogin];
//        UIButton * button2 = (UIButton *)[self viewWithTag:100];
//        button2.selected = YES;
//        [self remberPassWord];
//        
//    }
//}
//- (void)loginWith:(UIButton *)button
//{
//    if(self.accountTF.text.length == 0){
//        [MBProgressHUD showError:@"请输入账号和密码" time:1.5];
//    }else if(self.passwordTF.text.length == 0 && self.accountTF.text.length != 0){
//        [MBProgressHUD showError:@"请输入密码" time:1.5];
//    }else{
//        [self remberPassWord];
//        
//       [MBProgressHUD showMessage:@"登录中..."];
//        
//        NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
//        NSString * autoLogin = [defaults objectForKey:AutoLogin];
//        if([autoLogin isEqualToString:@"1"]){
//            [defaults setObject:@"AutoLogin" forKey:@"AutoLogin"];//值为autoLogin时自动登录  其他不自动登录
//        }
//        
//        BHMainTabbarController * tabbarVC = [[BHMainTabbarController alloc]init];
//        self.window.rootViewController = tabbarVC;
//    }
//}
//-(void)remberPassWord{
//    NSUserDefaults * defaul = [NSUserDefaults standardUserDefaults];
//    NSString * rember = [defaul objectForKey:RemberPassword];
//    NSString * autoLogin = [defaul objectForKey:AutoLogin];
//    if([rember isEqualToString:@"1"]||[autoLogin isEqualToString:@"1"]){
//        //账号
//        [defaul setObject:self.accountTF.text forKey:Account];
//        //密码
//        [defaul setObject:self.passwordTF.text forKey:Password];
//    }else{
//        //账号
//        [defaul setObject:nil forKey:Account];
//        //密码
//        [defaul setObject:nil forKey:Password];
//    }
//}

@end
